package com.adinnet.admin.schoolInform.service.impl;

import com.adinnet.admin.common.service.async.AsyncHandlerTask;
import com.adinnet.admin.schoolInform.model.SchoolInform;
import com.adinnet.admin.schoolInform.mapper.SchoolInformMapper;
import com.adinnet.admin.schoolInform.model.form.SchoolInformForm;
import com.adinnet.admin.schoolInform.model.vo.SchoolInformReq;
import com.adinnet.admin.schoolInform.model.vo.SchoolInformResp;
import com.adinnet.admin.schoolInform.service.SchoolInformService;
import com.adinnet.common.jpush.PushBean;
import com.adinnet.common.utils.PropertiesConfig;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 校区消息（管理端菜单） 服务实现类
 * </p>
 *
 * @author chenjia
 * @since 2020-12-21
 */
@Service
public class SchoolInformServiceImpl extends ServiceImpl<SchoolInformMapper, SchoolInform> implements SchoolInformService {

    @Autowired
    private  SchoolInformMapper schoolInformMapper;

    @Autowired
    private AsyncHandlerTask asyncHandlerTask;

    @Override
    public PageResult<SchoolInformResp> queryList(SchoolInformForm schoolInformForm) {
        Page<SchoolInformResp> page = new Page<>(schoolInformForm.getPage(), schoolInformForm.getLimit());
        List<SchoolInformResp> list = baseMapper.queryList(page, schoolInformForm);
        return new PageResult<>(list, schoolInformForm.getTotal());
    }

    @Override
    public JsonResult deleteIds(String informIds) {
        schoolInformMapper.deleteIds(informIds);
        return JsonResult.ok("操作成功");
    }

    @Override
    public void sendInform(SchoolInformReq schoolInformReq,Integer type) {
        SchoolInform schoolInform = schoolInformMapper.selectById(schoolInformReq.getId());
        schoolInformMapper.saveSendInform(schoolInform.getTitle(),schoolInform.getContent(),schoolInformReq.getSchoolId(),type);
        //获取需要推送的极光id
        List<String>  jpushIds=schoolInformMapper.getPushId(schoolInformReq.getSchoolId());
        //发送极光推送
        if(jpushIds!=null&&!jpushIds.isEmpty()){
            String[] s = new String[jpushIds.size()];
            jpushIds.toArray(s);
            PushBean pushBean = new PushBean();
            pushBean.setAppKey(PropertiesConfig.pushAppKey);
            pushBean.setMasterSecret(PropertiesConfig.pushSecret);
            pushBean.setAlert(schoolInform.getContent());
            pushBean.setTitle(schoolInform.getTitle());
            pushBean.setExtras(null);
            asyncHandlerTask.sendSchoolInform(pushBean,s);
        }
    }
}
